package SocketServer.service;

import SocketServer.common.ReqEntity;
import SocketServer.entity.Dict;
import SocketServer.entity.OptLog;
import SocketServer.utils.XmlUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 接口实现类
 */
public class DictServiceImpl implements DictServiceApi {

    @Override
    public List<Dict> queryAllDict(ReqEntity reqEntity) {
        //获取以前的所有记录
        List<Dict> dictLists = XmlUtils.queryAllDistList();
        return dictLists;
    }

    /**
     * 查找单词意思
     * @param reqEntity
     * @return
     */
    @Override
    public Dict findDict(ReqEntity reqEntity) {
        //获取以前的所有记录
        List<Dict> dictLists = XmlUtils.queryAllDistList();
        String word = reqEntity.getJsonData().split( ":" )[0];
        Dict result = null;
        for (Dict dict: dictLists) {
            if(word.equals(dict.getWord())){
                result = dict;
            }
        }
        return result;
    }

    /**
     * 新增单词
     * @param reqEntity
     * @return
     */
    @Override
    public boolean insertDict(ReqEntity reqEntity) {
        List<Dict> dictLists = XmlUtils.queryAllDistList();
        String[] dataList = reqEntity.getJsonData().split( ":" );
        Dict newDict = new Dict();
        newDict.setWord( dataList[0] );
        newDict.setMeaning( dataList[1] );
        dictLists.add(newDict);
        XmlUtils.createDictList( dictLists );//覆盖记录
        return true;
    }

    /**
     * 删除单词
     * @param reqEntity
     * @return
     */
    @Override
    public boolean deleteDict(ReqEntity reqEntity) {
        List<Dict> dictLists = XmlUtils.queryAllDistList();
        String word = reqEntity.getJsonData().split( ":" )[0];
        for (int i = dictLists.size()-1; i >=0; i--) {
            if(word.equals( dictLists.get(i).getWord() )){
                dictLists.remove(i);
            }
        }
        XmlUtils.createDictList( dictLists );//覆盖记录
        return true;
    }

    /**
     * 更新单词
     * @param reqEntity
     * @return
     */
    @Override
    public boolean updateDict(ReqEntity reqEntity) {
        List<Dict> dictLists = XmlUtils.queryAllDistList();
        String word = reqEntity.getJsonData().split( ":" )[0];
        String meaning = reqEntity.getJsonData().split( ":" )[1];
        for (Dict dict: dictLists) {
            if(word.equals(dict.getWord())){
                dict.setMeaning( meaning );
            }
        }
        XmlUtils.createDictList( dictLists );//覆盖记录
        return true;
    }

    /**
     * 查询所有的操作列表
     * @param reqEntity
     * @return
     */
    @Override
    public List<OptLog> findOptLogList(ReqEntity reqEntity) {
        List<OptLog> optLogLists = XmlUtils.queryAllOptLogList();
        return optLogLists;
    }

    /**
     * 记录操作日志
     * @param reqEntity
     * @return
     */
    @Override
    public boolean saveOptLog(ReqEntity reqEntity){
        //获取以前的所有记录
        List<OptLog> optLogLists = XmlUtils.queryAllOptLogList();
        OptLog optLog = new OptLog();
        optLog.setIpAddr( reqEntity.getHost() );
        optLog.setPort( reqEntity.getPort() );
        optLog.setOptType( reqEntity.getSocketServerApi() );
        Date currentDate = new Date(System.currentTimeMillis());
        String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentDate);
        optLog.setOptTime( currentTime );
//        optLog.setOptResult( result );
        optLogLists.add( optLog );
        XmlUtils.createOptLogList( optLogLists );
        return true;
    }

}
